﻿using LibreHardwareMonitor.Hardware;

namespace DesktopToy.Core;

internal class DeviceTemperatureSensor
{
    public DeviceTemperatureSensor(IHardware hardware)
    {
        Hardware = hardware;
        Sensors = new List<ISensor>();
        foreach (var sensor in hardware.Sensors)
        {
            if (sensor.SensorType != SensorType.Temperature) continue;
            Sensors.Add(sensor);
        }
    }

    private readonly IHardware Hardware;
    private readonly List<ISensor> Sensors;

    // 只取多个传感器的最大温度
    public float GetTemperature(string? name = null)
    {
        Hardware.Update();

        var max = 0f;
        foreach (var sensor in Sensors)
        {
            if (name != null && sensor.Name != name) continue;
            if (!sensor.Value.HasValue) continue;
            if (sensor.Value.Value > max)
            { 
                max = sensor.Value.Value;
            }
        }

        return max;
    }
    
    // 统计多个传感器的平均温度
    // public float GetTemperature(string? name = null)
    // {
    //     Hardware.Update();
    //     
    //     var count = 0;
    //     var sum = 0f;
    //     foreach (var sensor in Sensors)
    //     {
    //         if (name != null && sensor.Name != name) continue;
    //         if (!sensor.Value.HasValue) continue;
    //         count++;
    //         sum += sensor.Value.Value;
    //     }
    //
    //     if (count == 0) return 0;
    //     return sum / count;
    // }
}